<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="4">
            <a-tooltip arrow-point-at-center>
              <template slot="title">
                <span>从此日期开始，没有过交易的客户</span>
              </template>
              <a-form-item label="开始日期">
                <j-date placeholder="请选择日期" v-model="queryParam.date" date-format="YYYY-MM-DD"></j-date>
              </a-form-item>
            </a-tooltip>
          </a-col>
          <a-col :md="6" :sm="4">
            <a-form-item label="客户代码">
              <a-input placeholder="请输入客户代码" v-model="queryParam.serial"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="4">
            <a-form-item label="客户名称">
              <a-input placeholder="请输入客户名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
      <a-button icon="reload" type="primary" @click="searchReset">重置</a-button>
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button
        icon="download"
        type="primary"
        @click="handleExportXls(`长时间未交易供应商+${dayjs().format('YYYY-MM-DD-HH-mm-ss')}`)"
        >导出
      </a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
      </a-table>
    </div>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { downFile, getAction } from '@api/manage'
import dayjs from 'dayjs'

export default {
  name: 'customWarning',
  mixins: [JeecgListMixin],
  data() {
    return {
      description: '物料管理页面',
      // 表头
      columns: [
        {
          title: '代码',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '供应商地址',
          align: 'center',
          dataIndex: 'address'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        }
      ],
      url: {
        list: '/sale/hfCustom/customWarningList',
        exportXlsUrl: '/sale/hfCustom/exportXls',
        date: '/system/hfSystemSetting/queryById'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.initDate()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    dayjs,
    initDictConfig() {},
    initDate() {
      getAction(this.url.date, { id: '1806609520401285121' }).then(res => {
        if (res.success) {
          let dateJson = JSON.parse(res.result.setting)
          this.queryParam.date = dateJson['custom_interval']
        }
      })
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleExportXls(fileName) {
      if (!fileName || typeof fileName != 'string') {
        fileName = '导出文件'
      }
      let param = this.getQueryParams()
      if (this.selectedRowKeys.length === 0) {
        param['selections'] = this.dataSource.map(record => record.id).toString()
      }
      console.log('导出参数', param)
      downFile(this.url.exportXlsUrl, param).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    }
  }
}
</script>

<style scoped></style>
